home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / rpg / crossfir.001 / crossfir~ / eutl / xfile / xfile.c < prev    next >
C/C++ Source or Header  |  1994-09-19  |  2KB  |  80 lines

  1. /*
  2.  *   eutl - A collection of useful libraries
  3.  *   xmalloc - file wrappers which use the errlib routines so clients
  4.  *             can assume the file routines succeeded.
  5.  *
  6.  *   (c) Copyright 1993,1994 Eric Anderson 
  7.  *
  8.  * My thanks to Geoffrey Collyer and Henry Spencer for providing the basis
  9.  * for this copyright.
  10.  *
  11.  * Permission is granted to anyone to use this software for any purpose on
  12.  * any computer system, and to alter it and redistribute it freely, subject
  13.  * to the following restrictions:
  14.  *
  15.  * 1. The authors are not responsible for the consequences of use of this
  16.  *    software, no matter how awful, even if they arise from flaws in it.
  17.  *
  18.  * 2. The origin of this software must not be misrepresented, either by
  19.  *    explicit claim or by omission.  Since few users ever read sources,
  20.  *    credits must appear in the documentation.
  21.  *
  22.  * 3. Altered versions must be plainly marked as such, and must not be
  23.  *    misrepresented as being the original software.  Since few users
  24.  *    ever read sources, credits must appear in the documentation.
  25.  *
  26.  * 4. This notice may not be removed or altered.
  27.  */
  28.  
  29. #include "xfile.h"
  30. #include <libc.h>
  31.  
  32. static ErrorFunction Erf = LongJmpErrorFunction;
  33. char *xfile_packagever = "XFile -- file wrappers V1.0";
  34. char *xfile_Eopen = "error on open";
  35. char *xfile_Eclose = "error on close";
  36. char *xfile_Eflush = "error on flush";
  37.  
  38. FILE *xfopen(char *filename,char *type)
  39. {
  40.   FILE *ret;
  41.  
  42.   ret = fopen(filename,type);
  43.   if (ret) return ret;
  44.   {
  45.     char *errortype;
  46.     switch (*type) {
  47.      case 'r': 
  48.       errortype = *(type+1) == '+' ? "updating" : "reading";
  49.       break;
  50.      case 'w': 
  51.       errortype = *(type+1) == '+' ? "trucating and updating" : "writing";
  52.       break;
  53.      case 'a':
  54.       errortype = *(type+1) == '+' ? "updating at EOF" : "appending";
  55.       break;
  56.      default:
  57.       errortype = "some unknown operation";
  58.     }
  59.     Erf(xfile_packagever,xfile_Eopen,
  60.     "Error opening %s for %s\n",filename,errortype);
  61.   }
  62.   return NULL;            /*NOTREACHED*/
  63. }
  64.  
  65. void xfclose(FILE *file)
  66. {
  67.   if (fclose(file)) {
  68.     Erf(xfile_packagever,xfile_Eclose,
  69.     "Error closing file: %s\n",strerror(errno));
  70.   }
  71. }
  72.  
  73. void xfflush(FILE *file)
  74. {
  75.   if (fflush(file)) {
  76.     Erf(xfile_packagever,xfile_Eflush,
  77.     "Error flushing file: %s\n",strerror(errno));
  78.   }
  79. }
  80.